(function() {
    'use strict';
    var checkedObject = {};
    /**
     * 公司部门列表  初始化设置
     * @type {Object}
     */
    var settings = {
        "sAjaxSource": window.globalHost + "/agent-sys/admin/department/index.do",
        "aoColumns": [{
            "mDataProp": function(obj) {
                var checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" >';
                if (checkedObject[obj.id]) {
                    checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" checked="checked">';
                }
                return '<div class="checkbox checkbox-danger" style="width:100%">' + checkboxItemStr + '<label for="checkbox-' + obj.id + '" style="width:80%"></label></div>'
            },
            "sTitle": "",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp":function (obj) {
                return "<a class='departmentName' data-departmentId="+obj.id+" href='javascript:;'>"+obj.departName+"</a>"
            },
            "sTitle": "部门名称",
            "sDefaultContent": ""
        }, {
            "mDataProp": "createTime",
            "sTitle": "创建时间",
            "sDefaultContent": ""
        }],
        "fnServerParams": function(aoData) {
            return common.getSearchData("form.departmentSearch",aoData);
        }
    };
    // $("body").on("click",".allClick",function(ev){
    //     var list = $("body").find("input[type='checkbox']");
    //     list.each(function(i,I){
    //         $(I).trigger('click')
    //     })
    // });
    var options = $.extend(true, {}, _dataTableSettings, settings);
    /**
     * 实例化公司部门列表插件
     * @type {[type]}
     */
    var $currentDataTable=$(".dataTables-example");
    var oTable = $currentDataTable.dataTable(options);
    /**
     * 绑定数据列表中的多选框
     * @param  {[type]} ev){                     var checkItem [description]
     * @return {[type]}       [description]
     */
    $currentDataTable.on("change", ".checkbox", function(ev) {
        var checkItem = $(this).find("input[type='checkbox']")[0];
        var checkId = checkItem.id.replace('checkbox-', '');
        if (checkItem.checked) {
            checkedObject[checkId] = true;
        } else {
            delete checkedObject[checkId];
        }
        ev.stopPropagation();
    });
    //单击添加部门按钮。弹框出现新增公司部门对话框 [新增公司部门弹框，common.openTempDialog 是一个公共的弹框。专门用户处理 "DOM模板弹框"]
    $("button.insertDepartmentButton").on('click', function(ev) {
        var sendData={};
        var dialogDom="<div class='dialogPageCenter'><input type='text' name='departName' id='departName' style='width:100%;' placeholder='请输入新部门名称...'></div>";
        common.openTempDialog({
            title:"添加部门",
            content:dialogDom,
            ajaxUrl:window.globalHost + "/agent-sys/admin/department/save.do",
            sendData:sendData,
            FailedMsg:"请输入部门名称",
            beforeSend:function (layerDocument) {
                var departNameVal=layerDocument.find("#departName").val();
                //组装发送数据，添加部门需要向后台传递的参数字段，departName：需要添加的部门名称
                sendData.departName=departNameVal;
                //返回发送条件，部门名称不能为空
                return !!departNameVal;
            },
            resOk:function () {
                //添加成功则刷新列表数据
                oTable.fnClearTable();
            }
        });
        ev.stopPropagation();
    });
    //双击列表数据中的部门名称。弹出修改公司部门对话框
    $currentDataTable.off("dblclick").on("dblclick",".departmentName",function (ev) {
        var sendData={};
        var $this=$(this);
        var selfDepartmentId=$this.attr("data-departmentId");
        var dialogDom="<div class='dialogPageCenter'><input type='text' name='departName' id='departName' style='width:100%;' placeholder='请输入新部门名称...'></div>";
        common.openTempDialog({
            title:"修改部门名称",
            content:dialogDom,
            ajaxUrl:window.globalHost + "/agent-sys/admin/department/update.do",
            sendData:sendData,
            FailedMsg:"请输入部门名称",
            beforeSend:function (layerDocument) {
                var departNameVal=layerDocument.find("#departName").val();
                //组装发送数据，修改部门名称需要向后台传递的参数字段，departName：需要修的部门名称，id：当前部门的id
                sendData.departName=departNameVal;
                sendData.id=selfDepartmentId;
                //返回发送条件，要更改的部门名称和id不能为空
                return !!selfDepartmentId && !!departNameVal;
            },
            resOk:function () {
                // 修改成功则刷新列表数据
                oTable.fnClearTable();
            }
        });
        ev.stopPropagation();
    });

    $("button.deleteDepartmentButton").on("click", function(ev) {
        common.deleteConfirm(batchDelete);
        ev.stopPropagation();
    });
    /**
     * 批量删除方法
     * @param  {[type]} data [description]
     * @return {[type]}      [description]
     */
    function batchDelete() {
        var ids = [];
        if ($.isEmptyObject(checkedObject)) {
            layer.alert("请选择需要删除的部门!");
            return;
        }
        $.each(checkedObject, function(i, I) {
            ids.push(i);
        });
        ids = ids.join();
        $.post(window.globalHost + '/agent-sys/admin/department/batchDelete.do', { ids: ids }).success(function(data) {
            var record = common.transformData(data);
            layer.alert(record.res.msg);
            /**
             * 删除成功之后，清除本地的checkbox缓存。
             * @type {Object}
             */
            checkedObject = {};
            /**
             * 批量删除成功之后，刷新列表
             */
            oTable.fnClearTable();
        });
    }
}).call(this);